import { MyModule} from '../../modules/my.js'
let myModule = new MyModule()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    count:0,
    hasUserInfo:false,
    avatarUrl:'',
    favorList:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getBookCount()
    this.hasGottenUserInfo()
    this.getFavor()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  study:function(){
    wx.navigateTo({
      url:"/pages/study/index"
    })
  },
  about:function(){
    wx.navigateTo({
      url: "/pages/about/index"
    })
  },

  hasGottenUserInfo: function () {
    wx.getSetting({
      success: (data) => {
        console.log(data)
        if (data.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: (data) => {
              this.setData({
                hasUserInfo: true,
                avatarUrl: data.userInfo.avatarUrl,
              })
            }
          })
        } else {
          this.setData({
            hasUserInfo: false,
            avatarUrl: '/images/my/my.png'
          })
        }
      }
    })
  },
  onGotUserInfo:function(e){
    
    if (e.detail.userInfo){
      // 授权成功
      this.setData({
        avatarUrl: e.detail.userInfo.avatarUrl,
        hasUserInfo:true
      })
    }
  },
  getBookCount(){
    myModule.getFavorCount(res => {
      this.setData({
        count: res.count
      })
    })
  },
  getFavor(){
    myModule.getFavor(res => {
      this.setData({
        favorList: res
      })
    })
  },
  clickPreview:function(e){
    console.log(e)
    let classic = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/classic-detail/index?type=' + classic.type + '&id=' + classic.id,
    })
  }
})